﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Algorithm_api.calculate.MapOverlay
{
    public class Overlay
    {
        public int[][] ChinaMap { get; } = new int[34][];
        public string[] colors = { "","#f46e65", "#76d0a3", "#7ec2f3", "#fa90ba"};
        public Dictionary<int, string> useColor = new Dictionary<int, string>();

        public Overlay()
        {

            ChinaMap[0] = new int[] {//澳门
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0};
            ChinaMap[1] = new int[] {//宁夏
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1 };
            ChinaMap[2] = new int[] {//广东
                0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 1, 0, 0 };
            ChinaMap[3] = new int[] {//甘肃
                0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 1, 1, 0, 1, 0, 0, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1 };
            ChinaMap[4] = new int[] {//广西
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                0, 1, 0, 0 };
            ChinaMap[5] = new int[] {//安徽
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
                1, 0, 1, 0 };
            ChinaMap[6] = new int[] { // 香港
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0};
            ChinaMap[7] = new int[] {//福建
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 1, 0 };
            ChinaMap[8] = new int[] {//北京
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[9] = new int[] {//河南
                0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 1, 0, 0, 0, 0, 1, 1, 0,
                0, 0, 0, 0 };
            ChinaMap[10] = new int[] {//云南
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 1, 0, 1, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[11] = new int[] {//新疆
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[12] = new int[] {//陕西
                0, 1, 0, 1, 0, 0, 0, 0, 0, 1,
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 1, 0, 0, 0, 0, 1, 0, 1,
                0, 0, 0, 1 };
            ChinaMap[13] = new int[] {//辽宁
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1 };
            ChinaMap[14] = new int[] {//四川
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                1, 0, 1, 0, 0, 0, 0, 1, 0, 0,
                0, 0, 0, 1, 0, 1, 0, 0, 0, 1,
                0, 0, 0, 0 };
            ChinaMap[15] = new int[] {//吉林
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1 };
            ChinaMap[16] = new int[] {//江苏
                0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                0, 0, 1, 0 };
            ChinaMap[17] = new int[] {//青海
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[18] = new int[] {//海南
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0};
            ChinaMap[19] = new int[] {//上海
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 0 };
            ChinaMap[20] = new int[] {//河北
                0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 0, 1, 0, 0, 0, 1, 0,
                0, 0, 0, 1 };
            ChinaMap[21] = new int[] {//黑龙江
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1 };
            ChinaMap[22] = new int[] {//山西
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 1 };
            ChinaMap[23] = new int[] {//西藏
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 1, 0, 0, 1, 0, 0, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[24] = new int[] {//天津
                0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[25] = new int[] {//贵州
                0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                0, 1, 0, 0 };
            ChinaMap[26] = new int[] {//台湾
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0};
            ChinaMap[27] = new int[] {//湖北
                0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
                0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                1, 1, 0, 0 };
            ChinaMap[28] = new int[] {//山东
                0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
                1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
            ChinaMap[29] = new int[] {//重庆
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 1, 0, 1, 0, 0,
                0, 1, 0, 0 };
            ChinaMap[30] = new int[] {//江西
                0, 0, 1, 0, 0, 1, 0, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
                0, 1, 1, 0 };
            ChinaMap[31] = new int[] {//湖南
                0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 1, 0, 1, 0, 1,
                1, 0, 0, 0 };
            ChinaMap[32] = new int[] {//浙江
                0, 0, 0, 0, 0, 1, 0, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                1, 0, 0, 0 };
            ChinaMap[33] = new int[] {//内蒙古
                0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
                1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0 };
        }


        public string[] MapOverlay()
        {
            string[] area = new string[34];
            int[] visited = new int[34];
            area[0] = colors[1];
            int index = 1;
            int color = 1;
            int k = 0;
            while(index < ChinaMap.Length)
            {
                while(color <= 4)
                {
                    if (index >= ChinaMap.Length) break;
                    k = 0;
                    while ((k < index) &&(visited[k]* ChinaMap[index][k] != color) ) k++;
                    if (k < index) color++;
                    else
                    {
                        visited[index] = color;
                        index++;
                        color = 1;
                    }
                }
                if(color > 4)
                {
                    index--;
                    color = visited[index] + 1;
                }
            }

            for(int i = 1; i < area.Length; i++)
            {
                area[i] = colors[visited[i]];
            }
            return area;
        }

    }
}
